package com.masterlu.leetcode.daily.greedy.simple;

import java.util.Stack;

/**
 * 392. 判断子序列
 * https://leetcode-cn.com/problems/is-subsequence/
 *
 * @date created in 2021/5/31 16:36
 */
public class S392Subsequence {

    public static void main(String[] args) {
        isSubsequence("b","c");
    }

    /**
     * 双栈实现
     * @param s
     * @param t
     * @return
     */
    public static boolean isSubsequence(String s, String t) {

        char[] tChar = t.toCharArray();
        char[] sChar = s.toCharArray();
        Stack<Character> tstack = new Stack<>();
        Stack<Character> sstack = new Stack<>();

        for (char c : tChar) {
            tstack.push(c);
        }
        for (char c : sChar) {
            sstack.push(c);
        }

        while (!sstack.isEmpty()){
            char stmp = sstack.peek();
            while (!tstack.isEmpty()){
                if (stmp==tstack.pop()){
                    sstack.pop();
                    break;
                }
            }
            if (tstack.isEmpty()){
                break;
            }
        }

        if (sstack.isEmpty()){
            return Boolean.TRUE;
        }
        return Boolean.FALSE;
    }

    /**
     *
     * @return
     */
    private boolean isSubsequence2(String s, String t){

        int i = 0 ,j = 0;
        while (i < s.length() && j < t.length()){
            if (s.charAt(i) ==  t.charAt(j)){
                i ++;
            }
            j++;
        }

        return i == s.length();
    }
}
